<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <el-card class="box-card">
    <h1 class="container">订单列表</h1>
    <el-card class="hander-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <div>
            <el-statistic title="今日订单总数">
              <template slot="formatter"> {{ allNum }} </template>
              <template slot="suffix">
                <i class="el-icon-s-flag" style="color: rgb(243, 138, 18)"></i>
              </template>
            </el-statistic>
          </div>
        </el-col>
        <el-col :span="8">
          <div>
            <el-statistic title="当前订单">
              <template slot="formatter"> {{ nowNum }} </template>
              <template slot="suffix">
                <i class="el-icon-s-flag" style="color: rgb(239, 25, 28)"></i>
              </template>
            </el-statistic>
          </div>
        </el-col>
        <el-col :span="8">
          <div>
            <el-statistic title="已完成订单">
              <template slot="formatter"> {{ successNum }} </template>
              <template slot="suffix">
                <i
                  class="el-icon-s-flag"
                  style="color: rgba(15, 249, 62, 0.784)"
                ></i>
              </template>
            </el-statistic>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <div class="manage-header">
      <!-- form 搜索区域 -->
      <el-form
        :inline="true"
        :model="SearchForm"
        class="demo-form-inline"
        ref="searchFormRef"
        size="small"
      >
        <el-form-item label="时间范围" prop="time">
          <el-date-picker
            v-model="SearchTimeValue"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="菜名" prop="name">
          <el-input
            v-model="SearchForm.name"
            placeholder="请输入菜名"
          ></el-input>
        </el-form-item>
        <el-form-item label="订单状态" prop="type">
          <el-select v-model="SearchForm.type" placeholder="请选择状态">
            <el-option label="已接单" value="1"></el-option>
            <el-option label="制作中" value="2"></el-option>
            <el-option label="已完成" value="3"></el-option>
            <el-option label="已退单" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="onSearchClear">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      style="width: 100%"
      align="center"
    >
      <el-table-column type="index" label="序号" :index="indexMethod">
      </el-table-column>

      <el-table-column prop="name" label="菜名"> </el-table-column>
      <el-table-column prop="price" label="价格"> </el-table-column>
      <el-table-column label="订单状态" prop="type">
        <template slot-scope="scope">
          <div>
            <el-dropdown>
              <span class="el-dropdown-link">
                <el-tag :type="FoodColorMap(scope.row.type)">
                  {{ FoodTypeMap(Number(scope.row.type)) }}
                </el-tag>
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="stateUrl(1, scope.row)"
                  >已接单</el-dropdown-item
                >
                <el-dropdown-item @click.native="stateUrl(2, scope.row)"
                  >制作中</el-dropdown-item
                >
                <el-dropdown-item @click.native="stateUrl(3, scope.row)"
                  >已完成</el-dropdown-item
                >
                <el-dropdown-item @click.native="stateUrl(4, scope.row)"
                  >已退单</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="overMessage" label="备注"> </el-table-column>
      <el-table-column prop="updateTime" label="更新时间"> </el-table-column>
      <el-table-column prop="address" label="操作" width="280">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="onDetail(scope.row)"
            icon="el-icon-receiving"
            >订单详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page_num"
      >
      </el-pagination>
    </div>
    <!-- 抽屉 -->
    <el-drawer
      title="订单详情"
      :visible.sync="isDrawerVisible"
      :direction="direction"
    >
      <div class="drawer">
        <el-steps direction="vertical" :active="Number(drawerData.type)">
          <el-step
            title="已接单"
            description="订单已接单，请注意查收"
          ></el-step>
          <el-step title="制作中" description="订单制作中"></el-step>
          <el-step
            title="已完成"
            description="订单已完成，请提醒顾客"
          ></el-step>
        </el-steps>
        <div class="drawer_neirong">
          <el-descriptions title="内容" direction="vertical" :column="2" border>
            <el-descriptions-item label="顾客">{{
              drawerData.userName
            }}</el-descriptions-item>
            <el-descriptions-item label="手机号">{{
              drawerData.userPhone
            }}</el-descriptions-item>
            <el-descriptions-item label="订单来源">
              <el-tag size="small">{{ drawerData.orderCome }}</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="合计（元）">
              {{ drawerData.allMoney }}
            </el-descriptions-item>
            <el-descriptions-item label="备注" :span="2">{{
              drawerData.overMessage
            }}</el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
    </el-drawer>
  </el-card>
</template>
    
  
  
  <script>
export default {
  created() {
    this.page_num = this.tableData.length;
    this.getUserList();
  },
  data() {
    return {
      // 抽屉
      isDrawerVisible: false,
      direction: "rtl",

      // header_card
      like: true,
      value1: 4154.564,
      value2: 1314,
      title: "新增用户",

      allNum: 0,
      nowNum: 0,
      successNum: 0,

      disabled: false,
      typeList: [],
      // 编辑弹窗状态
      isEidtModelVisible: false,
      // 新增弹窗
      isAddModelVisible: false,
      labelPosition: "right",
      url: "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
      srcList: [
        "https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg",
        "https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg",
      ],
      //抽屉数据
      drawerData: {},
      // 订单信息
      tableData: [],
      // 分页
      page_num: 0,
      pageSize: 10,
      currentPage: 1,
      // 搜索form
      SearchForm: {
        time: "",
        name: "",
        type: "",
      },
      // 编辑form
      EidtForm: {},

      editRules: {
        name: [{ required: true, message: "请输入菜名" }],
        price: [{ required: true, message: "请输入价格" }],
        specil: [{ required: true, message: "请选择特点" }],
      },
      // 搜索日期
      SearchTimeValue: [
        new Date(2000, 10, 10, 10, 10),
        new Date(2000, 10, 11, 10, 10),
      ],
    };
  },
  computed: {
    FoodTypeMap() {
      return (value) => {
        const data = {
          1: "已接单",
          2: "制作中",
          3: "已完成",
          4: "已退单",
        };
        return data[value];
      };
    },
    FoodColorMap() {
      return (value) => {
        const data = {
          1: "info",
          2: "warning",
          3: "success",
          4: "danger",
        };
        return data[value];
      };
    },
  },
  methods: {
    indexMethod(index) {
      return index + 1;
    },
    //订单状态设置
    async stateUrl(stateNum, value) {
      const { data: res } = await this.$http.post("/admin/orderCenter/switch", {
        id: value.id,
        type: stateNum,
      });
      if (res.code !== 200) {
        this.$message.error("订单状态编辑失败");
        return;
      }
      this.$message.success("订单状态编辑成功");
      this.getUserList();
    },
    // 订单详细
    onDetail(value) {
      this.isDrawerVisible = true;
      this.drawerData = value;
    },
    // 搜索
    onSearch() {
      this.$refs.searchFormRef.validate(async (valid) => {
        if (valid) {
          const { name, type } = this.SearchForm;
          const poiId = localStorage.getItem("poiId");
          const { data: res } = await this.$http.post(
            "/admin/orderCenter/searchList",
            {
              name,
              type,
              poiId,
              page: this.currentPage,
              limit: this.pageSize,
            }
          );
          if (res.code !== 200) {
            this.$message.error("查询失败");
            return;
          }
          this.tableData = res.data;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    // 刷新
    async getUserList() {
      const poiId = window.localStorage.getItem("poiId");
      const { data: res } = await this.$http.get("/admin/orderCenter/getList", {
        params: {
          poiId,
        },
      });
      if (res.code !== 200) {
        this.$message.error("刷新失败");
        return;
      }
      this.tableData = res.data;
      this.page_num = this.tableData.length;
      this.allNum = res.total;
      this.successNum = res.sucessCount;
      this.nowNum = res.total - res.sucessCount;
    },
    // 搜索重置
    onSearchClear() {
      this.$refs.searchFormRef.resetFields();
      this.getUserList();
    },
  },
};
</script>
    
<style scoped>
.hander-card {
  margin-bottom: 20px;
}
.container {
  text-align: left;
}
.manage-header {
  display: flex;
  justify-content: space-between;
}
.block {
  margin-top: 20px;
}
.drawer {
  height: 250px;
  padding: 20px;
}
.drawer_neirong {
  margin-top: 20px;
}
</style>
    